package com.dckj.auto.common.interceptor;

import com.dckj.auto.common.utils.DateUtils;
import com.dckj.auto.common.utils.JsonUtils;
import com.dckj.auto.common.utils.PublicCode;
import com.dckj.auto.common.utils.SendMsgUtil;
import com.dckj.auto.common.utils.ehcache.EHCacheUtils;
import com.xiaoleilu.hutool.setting.dialect.Props;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    private Props props = new Props("config.properties","UTF-8");
    private String loginOvertimeCacheKey = props.getStr("login_overtime_cache_key");
    private Integer loginOvertime = props.getInt("login_overtime");

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String dispatcherUrl = "/user/login.action";
        Object object = request.getSession().getAttribute("user_Id");
        String userId = null == object?"":object.toString();
        if(StringUtils.isEmpty(userId)) {
            request.getRequestDispatcher(dispatcherUrl).forward(request,response);
            return false;
        }
        if(StringUtils.isNotEmpty(userId)) { //再判断用户操作间隔时间
            EHCacheUtils cache = EHCacheUtils.getInstance();
            Object overtime = cache.get(loginOvertimeCacheKey, userId);
            String currentTime = DateUtils.getCurrentTime();
            if(null != overtime){
                cache.put(loginOvertimeCacheKey,userId,currentTime);
                String time = overtime.toString();
                Integer minute = DateUtils.getMinute(time,currentTime);
                if(minute>=loginOvertime){
                    cache.remove(loginOvertimeCacheKey,userId);
                    request.getSession().setAttribute(PublicCode.USER_ID, "");//把用户Id设置为空
                    request.getRequestDispatcher(dispatcherUrl).forward(request,response);
                    SendMsgUtil.sendJsonMessage(response,JsonUtils.error("登陆超时，请重新登陆"));
                    return false;
                }
            }
            cache.put(loginOvertimeCacheKey,userId,currentTime);
            return true;
        }
        String sessionToken = (String)request.getSession().getAttribute(PublicCode.LOGIN_KEY_TOKEN);
        if(StringUtils.isBlank(sessionToken)){
            request.getRequestDispatcher(dispatcherUrl).forward(request,response);
            return false;
        }

        return true;


    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }




}
